package com.wkf.truck.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkf.truck.common.CommonEnum;
import com.wkf.truck.common.R;
import com.wkf.truck.dto.OrderDto;
import com.wkf.truck.dto.OrderSearchDto;
import com.wkf.truck.model.Orders;
import com.wkf.truck.model.Truck;
import com.wkf.truck.model.User;
import com.wkf.truck.service.OrdersService;
import com.wkf.truck.service.TruckService;
import com.wkf.truck.service.UserService;
import com.wkf.truck.vo.OrdersVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UserService userService;

    @Autowired
    private TruckService truckService;

    /**
     * 获取订单列表
     *
     * @param orderDto
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody OrderDto orderDto) {

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", orderDto.getUserId());

        if (!StringUtils.isEmpty(orderDto.getStatus())) {
            wrapper.eq("status", orderDto.getStatus());
        }

        List<Orders> list = ordersService.list(wrapper);
        return R.ok().put("data", list);
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderDetails")
    public R getOrderDetails(@RequestParam("orderId") Long orderId) {
        Orders orders = ordersService.getOne(new QueryWrapper<Orders>().eq("id", orderId));

        OrdersVo ordersVo = new OrdersVo();
        BeanUtils.copyProperties(orders, ordersVo);
        User user = userService.getOne(new QueryWrapper<User>().eq("id", orders.getUserId()));

        if (user != null) {
            ordersVo.setUsername(user.getUsername());
            ordersVo.setPhone(user.getPhone());
        }
        return R.ok().put("data", ordersVo);
    }

    /**
     * 下订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/placeOrder")
    public R placeOrder(@RequestBody Orders orders) {
        ordersService.save(orders);
        //订单加一
        Long truckId = orders.getTruckId();
        Truck truck = truckService.getById(truckId);
        if (truck == null) {
            return R.error();
        }
        Long userId = truck.getUserId();

        User user = userService.getById(userId);
        if (user == null) {
            return R.error();
        }
        user.setOrderNumber(user.getOrderNumber() == null ? 1 : user.getOrderNumber().intValue() + 1);
        userService.saveOrUpdate(user);

        return R.ok();
    }

    /**
     * 商家获取订单列表
     *
     * @param orderSearchDto
     * @return
     */
    @PostMapping("/getOrders")
    public R getOrders(@RequestBody OrderSearchDto orderSearchDto) {

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(orderSearchDto.getStatus())) {
            if (orderSearchDto.getStatus().equals(CommonEnum.OrderStatus.待处理.getCode())) {
                wrapper.eq("status", CommonEnum.OrderStatus.待处理.getCode());
            } else if (orderSearchDto.getStatus().equals(CommonEnum.OrderStatus.已取消.getCode())) {
                wrapper.eq("status", CommonEnum.OrderStatus.已取消.getCode());
            } else if (orderSearchDto.getStatus().equals(CommonEnum.OrderStatus.已完成.getCode())) {
                wrapper.eq("status", CommonEnum.OrderStatus.已完成.getCode());
            }
        }

        //商家
        if (orderSearchDto.getRoleCode().equals(CommonEnum.Role.商家.getCode())) {
            //查出商家truckId
            User user = userService.getById(orderSearchDto.getUserId());
            if (user == null) {
                return R.error("该商家不存在");
            }
            Long truckId = user.getTruckId();

            wrapper.eq("truck_id", truckId);
        }
        //顾客
        else {
            wrapper.eq("user_id", orderSearchDto.getUserId());
        }

        List<Orders> ordersList = ordersService.list(wrapper);
        return R.ok().put("data", ordersList);
    }

    /**
     * 取消订单
     *
     * @param orderId
     * @return
     */
    @GetMapping("/cancelOrder")
    public R cancelOrder(@RequestParam("orderId") Long orderId) {

        Orders orders = ordersService.getById(orderId);

        if (orders == null) {
            return R.error("没有该订单");
        }
        orders.setStatus(CommonEnum.OrderStatus.已取消.getCode());
        ordersService.saveOrUpdate(orders);
        return R.ok();
    }

    /**
     * 完成订单
     *
     * @param orderId
     * @return
     */
    @GetMapping("/finishOrder")
    public R finishOrder(@RequestParam("orderId") Long orderId) {
        Orders orders = ordersService.getById(orderId);
        orders.setStatus(CommonEnum.OrderStatus.已完成.getCode());

        ordersService.saveOrUpdate(orders);
        return R.ok();
    }

    @PostMapping("/getAllOrders")
    public R getAllOrders(@RequestBody Orders orders){

        IPage page = ordersService.getAllOrders(orders);
        return R.ok(page);
    }
}
